﻿// Catalog class.
// Do not manually modify this generated file.
using System;
using System.Configuration;
using System.Data;

using BizElements.Core;

namespace BizElements.BusinessLayer
{
	/// <summary>
	/// Catalog used by AppLog.
	/// Reads information from *.config file.
	/// </summary>
	[Serializable]
	internal sealed class AppLogCatalog : ICatalog
	{
		private static readonly DbmsType dbmsType;
		private static readonly string connectionString;

		static AppLogCatalog()
		{
            string cfgDbmsType = ConfigurationManager.AppSettings["BizElements.BusinessLayer.AppLog.DbmsType"];
			if (cfgDbmsType != null)
				dbmsType = (DbmsType)Enum.Parse(typeof(DbmsType), cfgDbmsType);

            if ((ConfigurationManager.ConnectionStrings != null) && (ConfigurationManager.ConnectionStrings["BizElements.BusinessLayer.AppLog.ConnectionString"] != null))
                connectionString = ConfigurationManager.ConnectionStrings["BizElements.BusinessLayer.AppLog.ConnectionString"].ConnectionString;
		}

		/// <summary>
		/// Gets database type.
		/// </summary>
		public DbmsType DbmsType
		{
			get { return dbmsType; }
		}

		/// <summary>
		/// Gets connection string.
		/// </summary>
		public string ConnectionString
		{
			get { return connectionString; }
		}

		/// <summary>
		/// Creates a connection provider for this catalog.
		/// </summary>
		/// <returns>Connection provider that connects to this catalog.</returns>
		public IConnectionProvider CreateConnectionProvider()
		{
			return ConnectionProviderFactory.CreateConnectionProvider(this.DbmsType, this.ConnectionString);
		}

        const string TableNameOverrideKeyFormat = "BizElements.BusinessLayer.{0}.TableName";

		/// <summary>Gets overriden/obfuscated table name from application's configuration file.</summary>
		internal static string GetTableNameOverride(string tableName)
		{
			string settingsKey = string.Format(TableNameOverrideKeyFormat, tableName);
			return ConfigurationManager.AppSettings[settingsKey];
		}

        const string ColumnNameOverrideKeyFormat = "BizElements.BusinessLayer.{0}.{1}.ColumnName";

		/// <summary>Gets overriden/obfuscated column name from application's configuration file.</summary>
		internal static string GetColumnNameOverride(string tableName, string columnName)
		{
			string settingsKey = string.Format(ColumnNameOverrideKeyFormat, tableName, columnName);
			return ConfigurationManager.AppSettings[settingsKey];
		}
	}
}